﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System;
using System.ComponentModel.Composition;
using Peter.Common.Dialog;
using Peter.Common.Interfaces;

namespace Peter.Common.Services
{
   /// <summary>
   /// Service used to request text input.
   /// </summary>
   [Export (typeof (ITextInputRequestService))]
   [PartCreationPolicy (CreationPolicy.Shared)]
   public class TextInputRequestService : ITextInputRequestService
   {
      /// <summary>
      /// Gets or Sets the peter reference.
      /// </summary>
      [Import (typeof (IPeter))]
      public IPeter Peter { get; set; }

      /// <summary>
      /// Requests the user to input a password.
      /// </summary>
      /// <param name="prompt">The prompt to display to the users.</param>
      /// <param name="title">The title for the dialog.</param>
      /// <param name="canOk">A function that checks if ok can be pressed or not</param>
      /// <param name="placeholder">Text used for a placeholder when no input is present.</param>
      /// <returns>The text that was input by the user.</returns>
      public string Request (string prompt, string title, Func<string, bool> canOk = null, string placeholder = "")
      {
         var model = new ModelTextInputRequest { Prompt = prompt, Title = title, Placeholder = placeholder, CanOkCheck = canOk};
         var res = this.Peter.ShowDialog (model);
         if (res == DialogCloseAction.Ok)
            return model.Input;
         return string.Empty;
      }
   }

   /// <summary>
   /// Interface for text input request service.
   /// </summary>
   public interface ITextInputRequestService
   {
      /// <summary>
      /// Requests the user to input a password.
      /// </summary>
      /// <param name="prompt">The prompt to display to the users.</param>
      /// <param name="title">The title for the dialog.</param>
      /// <param name="canOk">A function that checks if ok can be pressed or not</param>
      /// <param name="placeholder">Text used for a placeholder when no input is present.</param>
      /// <returns>The text that was input by the user.</returns>
      string Request (string prompt, string title, Func <string, bool> canOk = null, string placeholder = "");
   }
}
